/**
 * 
 */
package ru.olympus.antonyhands.ao.client;

import ru.olympus.antonyhands.ao.client.widgets.AddItemBaseDialogBox;
import ru.olympus.antonyhands.ao.client.widgets.AddItemVerticalPanel;
import ru.olympus.antonyhands.ao.client.widgets.CheapestWayToGetItem;
import ru.olympus.antonyhands.ao.client.widgets.ItemByCategoryInfoPanel;
import ru.olympus.antonyhands.ao.client.widgets.ItemByCraftSkillInfoPanel;
import ru.olympus.antonyhands.ao.shared.util.StringUtilities;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author AntonyHands
 * 
 */
public class UIHelper
{
    public static final String INFO_DIV = "infoDiv";

    public static final MenuBar createMainMenu(final AOHelperServiceAsync service)
    {
        MenuBar mainMenu = new MenuBar();

        MenuBar itemsList = new MenuBar(true);
        mainMenu.addItem("Список предметов", itemsList);

        itemsList.addItem("По типу предмета", new Command()
        {
            @Override
            public void execute()
            {
                RootPanel.get(INFO_DIV).clear();
                RootPanel.get(INFO_DIV).add(new ItemByCategoryInfoPanel(service));
            }
        });
        itemsList.addItem("По навыку производства", new Command()
        {
            @Override
            public void execute()
            {
                RootPanel.get(INFO_DIV).clear();
                RootPanel.get(INFO_DIV).add(new ItemByCraftSkillInfoPanel(service));
            }
        });

        MenuBar analysys = new MenuBar(true);
        mainMenu.addSeparator();
        mainMenu.addItem("Анализ", analysys);
        
        analysys.addItem("Самый дешёвый способ получить предмет", new Command() {
			@Override
			public void execute() {
				RootPanel.get(INFO_DIV).clear();
                RootPanel.get(INFO_DIV).add(new CheapestWayToGetItem(service));
			}
		});

        MenuBar storage = new MenuBar(true);
        mainMenu.addSeparator();
        mainMenu.addItem("Хранилище", storage);

        MenuBar additionMenu = new MenuBar(true);
        storage.addItem("Добавить", additionMenu);

        additionMenu.addItem("Добавить категорию", new Command()
        {
            @Override
            public void execute()
            {
                new AddItemBaseDialogBox(AOHelperServiceAsync.ITEM_CATEGORY, service, "Добавить категорию").center();
            }
        });
        additionMenu.addItem("Добавить производственное умение", new Command()
        {
            @Override
            public void execute()
            {
                new AddItemBaseDialogBox(AOHelperServiceAsync.CRAFT_SKILL, service, "Добавить производственное умение").center();
            }
        });
        additionMenu.addItem("Добавить предмет", new Command()
        {
            @Override
            public void execute()
            {
                RootPanel.get(INFO_DIV).clear();
                RootPanel.get(INFO_DIV).add(new AddItemVerticalPanel(service));
//                new AddItemDialogBox(service).center();
            }
        });

        storage.addItem("Сохранить", new Command()
        {
            @Override
            public void execute()
            {
                service.saveData(new AsyncCallback<String>()
                {
                    @Override
                    public void onFailure(Throwable caught)
                    {
                        Window.alert("Не удалось выполнить сохранение данных.");
                    }

                    @Override
                    public void onSuccess(String result)
                    {
                        if (StringUtilities.isEmpty(result))
                            Window.alert("Данные успешно сохранены.");
                        else
                            Window.alert(result);
                    }
                });
            }
        });

        return mainMenu;
    }

}
